﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;

namespace Client
{
    public partial class DangKy : Form
    {
        public delegate void RegisterHanler(string tk, string pass, string truso, string diachi, string dienthoai, string email);
        public event RegisterHanler RegisterEvent;

        public DangKy()
        {
            InitializeComponent();
        }


        //Close form
        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            this.Close();
        }


        //Kiem tra loi du lieu nhap
        private int KiemTraLoi()
        {
            if (txtDiaChi.Text.Trim() == "" || txtDienThoai.Text.Trim() == "" || txtEmail.Text.Trim() == "" || txtMatKhau.Text.Trim() == "" || txtTaiKhoan.Text.Trim() == "" || txtTruSo.Text.Trim() == "")
            {
                return 1;
            }
            int sdt = 0;
            bool f = int.TryParse(txtDienThoai.Text, out sdt);
            if (f == false)
                return 2;
            f = IsValidEmail(txtEmail.Text);
            if (f == false)
                return 3;
            return 0;
         
        }

        //Kiem tra dinh dang email
        public bool IsValidEmail(string email)
        {
            string pattern = @"^[-a-zA-Z0-9][-.a-zA-Z0-9]*@[-.a-zA-Z0-9]+(\.[-.a-zA-Z0-9]+)*\.
 (com|edu|info|gov|int|mil|net|org|biz|name|museum|coop|aero|pro|tv|[a-zA-Z]{2})$";

            Regex check = new Regex(pattern, RegexOptions.IgnorePatternWhitespace);

            bool valid = false;
            if (string.IsNullOrEmpty(email))
            {
                valid = false;
            }
            else
            {
                valid = check.IsMatch(email);
            }
            return valid;
        }

        //Dang ky
        private void btnDangKy_Click(object sender, EventArgs e)
        {
            int flag = KiemTraLoi();

            if (RegisterEvent != null && flag == 0)
            {
                RegisterEvent(txtTaiKhoan.Text, txtMatKhau.Text, txtTruSo.Text, txtDiaChi.Text, txtDienThoai.Text, txtEmail.Text);
            }
            else
            {
                if (flag == 1)
                    MessageBox.Show("Không được để trống dữ liệu.");
                if (flag == 2)
                    MessageBox.Show("Số điện thoại không hợp lệ.");
                if (flag == 3)
                    MessageBox.Show("Email không hợp lệ.");
            }
        }
        
    }
}
